uniform sampler2D tex, alpha;
varying vec3 normal;
varying vec3 lightDir;

void main(void) {
    vec4 Am = vec4(texture2D(tex, gl_TexCoord[0].st).rgb, texture2D(alpha, gl_TexCoord[0].st).r);
    vec4 Dm = Am;

    normal = normalize(normal);
    lightDir = normalize(lightDir);
    float lambertTerm = dot(normal, lightDir);

    vec4 ambient = Am*gl_LightSource[0].ambient + Am*gl_LightModel.ambient;
    vec4 diffuse = Dm*gl_LightSource[0].diffuse*lambertTerm;

    vec4 fogColour = vec4(0.3, 0.3, 0.3, 1.0);
    float z = gl_FragCoord.z / gl_FragCoord.w;
    float fogFactor = exp(-0.003*z);
    fogFactor = clamp(fogFactor, 0.0, 1.0);
    gl_FragColor = mix(fogColour, ambient+diffuse, fogFactor);
}
